package com.zhentao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.dto.ParamRuleDTO;
import com.zhentao.dto.ParamRuleQueryDTO;
import com.zhentao.dto.RuleExecuteRequestDTO;
import com.zhentao.service.ParamRuleService;
import com.zhentao.util.Result;
import com.zhentao.vo.ParamRuleVO;
import com.zhentao.vo.RuleExecuteResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Tag(name = "参数规则", description = "规则引擎接口")
@RestController
@RequestMapping("/param/rule")
@RequiredArgsConstructor
@Slf4j
public class ParamRuleController {

    private final ParamRuleService paramRuleService;

    @Operation(summary = "分页查询规则")
    @GetMapping("/page")
    public Result page(ParamRuleQueryDTO query) {
        Page<ParamRuleVO> data = paramRuleService.queryRules(query);
        return Result.OK(data);
    }

    @Operation(summary = "规则详情")
    @GetMapping("/{id}")
    public Result detail(@PathVariable Long id) {
        return Result.OK(paramRuleService.getRuleDetail(id));
    }

    @Operation(summary = "创建规则")
    @PostMapping
    public Result create(@RequestBody ParamRuleDTO dto) {
        return Result.OK(paramRuleService.createRule(dto));
    }

    @Operation(summary = "更新规则")
    @PutMapping("/{id}")
    public Result update(@PathVariable Long id, @RequestBody ParamRuleDTO dto) {
        return Result.OK(paramRuleService.updateRule(id, dto));
    }

    @Operation(summary = "删除规则")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        return Result.OK(paramRuleService.deleteRule(id));
    }

    @Operation(summary = "切换规则状态")
    @PutMapping("/{id}/status/{status}")
    public Result toggle(@PathVariable Long id, @PathVariable Integer status) {
        return Result.OK(paramRuleService.toggleRuleStatus(id, status));
    }

    @Operation(summary = "执行规则")
    @PostMapping("/execute")
    public Result execute(@RequestBody RuleExecuteRequestDTO request) {
        RuleExecuteResultVO data = paramRuleService.executeRule(request);
        return Result.OK(data);
    }
}


